package com.liu.qqRobot.poke.callable;

import cn.hutool.core.util.ObjUtil;
import com.liu.mybatisPlus.poke.entity.base.PokeSkill;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class SyncPokeSkillCallable implements Callable<List<PokeSkill>> {

    private final Element body;

    private final String region;

    private final CountDownLatch countDownLatch;

    private final Map<Integer, PokeSkill> existPokeSkillMap;

    public SyncPokeSkillCallable(Element body, String region, CountDownLatch countDownLatch, Map<Integer, PokeSkill> existPokeSkillMap) {
        this.body = body;
        this.region = region;
        this.countDownLatch = countDownLatch;
        this.existPokeSkillMap = existPokeSkillMap;
    }

    @Override
    public List<PokeSkill> call() throws Exception {
        List<PokeSkill> pokeSkillList = new ArrayList<>();
        try {
            Elements select = body.select("table.hvlist.a-c.roundy.bg-" + region + ".bd-" + region + ".sortable.fulltable");
            Element element = select.get(0);
            Elements tbody = element.getElementsByTag("tbody");
            Element element1 = tbody.get(0);
            Elements tr = element1.getElementsByTag("tr");
            for (Element element2 : tr) {
                Elements td = element2.getElementsByTag("td");
                if (td != null && td.size() > 0) {
                    Element element4 = td.get(0);
                    String wikiIdtext = element4.text();
                    try {
                        Integer.valueOf(wikiIdtext);
                    } catch (NumberFormatException e) {
                        log.error("同步宝可梦招式，解析wikiId失败，失败原因：{}", e.getMessage(), e);
                        continue;
                    }
                    PokeSkill pokeSkill = existPokeSkillMap.get(Integer.valueOf(wikiIdtext));
                    if (pokeSkill == null) {
                        pokeSkill = new PokeSkill();
                        pokeSkill.setWikiId(Integer.valueOf(wikiIdtext));
                    }
                    for (int i = 0; i < td.size(); i++) {
                        Element element3 = td.get(i);
                        if (i == 1) {
                            String text = element3.getElementsByTag("a").text();
                            pokeSkill.setName(text);
                        }
                        if (i == 4) {
                            String text = element3.getElementsByTag("a").text();
                            pokeSkill.setAttribute(text);
                        }
                        if (i == 5) {
                            String text = element3.getElementsByTag("a").text();
                            pokeSkill.setClassification(text);
                        }
                    }
                    if (ObjUtil.isAllNotEmpty(pokeSkill)) {
                        log.info("同步宝可梦招式 wikiId：{}", pokeSkill.getWikiId());
                        pokeSkillList.add(pokeSkill);
                    }
                }
            }
        } catch (Exception e) {
            log.error("同步宝可梦招式线程{}同步失败，失败原因：{}", Thread.currentThread().getName(), e.getMessage(), e);
        } finally {
            countDownLatch.countDown();
        }
        return pokeSkillList;
    }
}
